package com.wuwangfu.topn;

import com.alibaba.fastjson2.JSON;
import com.wuwangfu.entity.Behavior;
import com.wuwangfu.entity.ItemEventCount;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @Description：
 * @Author：jcshen
 * @Date：2023-07-04
 *
 *
 * 需求说明：统计一定时间段内的，热门商品/品牌TopN
 *          按照EventTime划分滑动窗口，窗口长度为10分钟，1分钟滑动一次
 *          将数据进行增量聚合，窗口出触发后还要进行排序(reduce，WindowFunction)，在WindowFunction使用onTime进行排序
 *
 *
 * 数据
 *  {"userId": "u001", "itemId": "p1001", "categoryId": "c11", "type": "pv", "timestamp": "2020-03-08 11:11:11"}
 *  {"userId": "u002", "itemId": "p1001", "categoryId": "c11", "type": "pv", "timestamp": "2020-03-08 11:11:11"}
 *  {"userId": "u003", "itemId": "p1001", "categoryId": "c11", "type": "pv", "timestamp": "2020-03-08 11:11:11"}
 *  {"userId": "u003", "itemId": "p1001", "categoryId": "c11", "type": "cart", "timestamp": "2020-03-08 11:11:11"}
 *  {"userId": "u011", "itemId": "p2222", "categoryId": "c22", "type": "pv", "timestamp": "2020-03-08 11:11:11"}
 *  {"userId": "u012", "itemId": "p2222", "categoryId": "c22", "type": "pv", "timestamp": "2020-03-08 11:11:11"}
 *  {"userId": "u012", "itemId": "p2222", "categoryId": "c22", "type": "pv", "timestamp": "2020-03-08 11:12:01"}
 *  {"userId": "u001", "itemId": "p1001", "categoryId": "c11", "type": "pv", "timestamp": "2020-03-08 11:12:01"}
 *  {"userId": "u002", "itemId": "p1001", "categoryId": "c11", "type": "pv", "timestamp": "2020-03-08 11:12:01"}
 *  {"userId": "u003", "itemId": "p1001", "categoryId": "c11", "type": "pv", "timestamp": "2020-03-08 11:12:01"}
 *  {"userId": "u003", "itemId": "p1001", "categoryId": "c11", "type": "cart", "timestamp": "2020-03-08 11:12:01"}
 *  {"userId": "u011", "itemId": "p2222", "categoryId": "c22", "type": "pv", "timestamp": "2020-03-08 11:12:01"}
 *  {"userId": "u012", "itemId": "p2222", "categoryId": "c22", "type": "pv", "timestamp": "2020-03-08 11:12:01"}
 *  {"userId": "u011", "itemId": "p2222", "categoryId": "c22", "type": "pv", "timestamp": "2020-03-08 11:13:01"}
 *
 *
 */
public class HotGoodsTopN {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //选择EventTime作为时间语义
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        //设置checkpoint时间
        env.enableCheckpointing(60000);
        //设置并行度
        env.setParallelism(1);
        //获取数据
        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        //调用底层的Process（可做类似map的操作）
        SingleOutputStreamOperator<Behavior> process = lines.process(new ProcessFunction<String, Behavior>() {
            @Override
            public void processElement(String value,Context ctx, Collector<Behavior> out) throws Exception {
                try {
                    // FastJson 会自动把时间解析成long类型的TimeStamp
                    Behavior behavior = JSON.parseObject(value, Behavior.class);
                    out.collect(behavior);
                } catch (Exception e) {
                    e.printStackTrace();
                    //TODO 记录异常出现的次数
                }
            }
        });

        //提取EventTime，转换成Timestamp格式，生成watermark
        SingleOutputStreamOperator<Behavior> behaviorWatermark = process
                .assignTimestampsAndWatermarks(
                new BoundedOutOfOrdernessTimestampExtractor<Behavior>(Time.seconds(0)) {
            @Override
            public long extractTimestamp(Behavior element) {
                //设定延迟时间
                return element.timestamp;
            }
        });

        //按照指定事件分组：某个商品，在窗口时间内，被（点击、购买、加购、收藏）了多少次
        KeyedStream<Behavior, Tuple> keyed = behaviorWatermark.keyBy("itemId", "type");

        //把分好组的数据，划分窗口：假设窗口总长10分钟，步长1分钟滑动一次
        WindowedStream<Behavior, Tuple, TimeWindow> window = keyed
                .window(SlidingEventTimeWindows.of(Time.minutes(10), Time.minutes(1)));

        //窗口内的数据进行聚合，根据窗口的开始时间和窗口的结束时间
        SingleOutputStreamOperator<ItemEventCount> result = window
                .apply(new WindowFunction<Behavior, ItemEventCount, Tuple, TimeWindow>() {
            @Override
            public void apply(Tuple tuple, TimeWindow window, Iterable<Behavior> input, Collector<ItemEventCount> out) throws Exception {
                //获取分组字段
                String itemId = tuple.getField(0);
                String type = tuple.getField(1);
                //获取窗口的开始和结束时间
                long start = window.getStart();
                long end = window.getEnd();
                //累加
                int count = 0;
                for (Behavior bean : input) {
                    count += 1;
                }
                //输出结果
                out.collect(ItemEventCount.of(itemId, type, start, end, count));
            }
        });

        result.print();
        env.execute();
    }
}
